package com.globagames.strategicgames.command
{
	
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.globagames.strategicgames.business.*;
	import com.globagames.strategicgames.event.*;
	import com.globagames.strategicgames.model.*;
	
	import mx.rpc.IResponder;
	

	public class CompanyExchangeCommand implements Command, IResponder
	{
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		
		private var companyDelegate : CompanyDelegate;
		private var sessionDelegate : SessionDelegate;
		private var companyEvent:CompanyExchangeEvent;
		private var currentTask:String;
		public function CompanyExchangeCommand()
		{		
		}
	
		public function execute( event : CairngormEvent ): void
		{	
			companyEvent = event as CompanyExchangeEvent;
			currentTask = companyEvent.action;
			if(companyEvent.action=='create'){			
				companyDelegate = new CompanyDelegate( this );				
				companyDelegate.doCreate(companyEvent.company);		
			}
			if(companyEvent.action=='remove'){			
				companyDelegate = new CompanyDelegate( this );				
				companyDelegate.doRemove(model.company);		
			}
			
			if(companyEvent.action=='find'){
				companyDelegate = new CompanyDelegate( this );
				companyDelegate.doFind(companyEvent.company);				
			}	
			
			if(companyEvent.action=='addSoldier'){
				companyDelegate = new CompanyDelegate( this );
				companyDelegate.doAddSoldier(companyEvent.soldier, companyEvent.company);				
			}
		}
		public function result( event : Object ) : void
		{	
			if(currentTask=='create'){
				var company : Company = Company( event.result );	
				trace(company.name +" created");			 			
				//model.session.state = "READY";
				//currentTask = "session-commit";
				//sessionDelegate = new SessionDelegate( this );
				//sessionDelegate.doCommit();
				companyEvent.isCreated = true;
			} else
			if(currentTask=='find'){
				var company : Company = Company( event.result );	
				companyEvent.company = company;
				companyEvent.isFound = true;
			}
			if(currentTask=='addSoldier'){
				companyEvent.isSoldierAdded = true;
			}
		}
		
		public function fault( event : Object ) : void
		{
			trace(event.fault.rootCause.message);//, event.fault.faultCode);
		}
		
		
		
	}

}